package com.lemner.ke.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemner.ke.R;
import com.lemner.ke.adapter.VideoFragmentAdapter;
import com.lemner.ke.base.BaseActivity;
import com.lemner.ke.base.URL;
import com.lemner.ke.bean.VideoRvBean;
import com.lemner.ke.fragment.VideoDescFragment;
import com.lemner.ke.fragment.VideoTabFragment;
import com.lemner.ke.util.LoadImageUtils;
import com.lemner.ke.view.MyAppBarChangeListener;

import java.util.ArrayList;
import java.util.List;

public class VideoDetailActivity extends BaseActivity {
    private List<Fragment> fragmentList;
    private List<String> titleList;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private VideoFragmentAdapter adapter;
    private ImageView topbar_back;
    private VideoRvBean videoRvBean;
    private CollapsingToolbarLayout ctl_videodetail;
    private AppBarLayout abl_videodetail;
    private ImageView iv_video_image;
    private String titleStr = "";
    private TextView tv_video_title;
    private TextView tv_video_desc;
    private String descStr = "";
    private String coverImg = "";
    @Override
    public int getLayoutFile() {
        return R.layout.activity_video_detail;
    }

    @Override
    public void initView() {
        initFullScreen();
//        initImmersionBarForTopBar(findViewById(R.id.ctl_videodetail));
        viewPager=findViewById(R.id.vp_videodetail_viewpager);
        tabLayout=findViewById(R.id.tl_videodetail_tablayout);
        tabLayout=findViewById(R.id.tl_videodetail_tablayout);
        topbar_back=findViewById(R.id.toolbar_right_menu);
        tv_video_title=findViewById(R.id.tv_video_title);
        tv_video_desc=findViewById(R.id.tv_vide_desc);
        ctl_videodetail=findViewById(R.id.ctl_videodetail);
        abl_videodetail=findViewById(R.id.videodetail_app_bar);
        iv_video_image=findViewById(R.id.iv_videodetail_bar);

        if (intent != null) {
            videoRvBean = (VideoRvBean) intent.getSerializableExtra("bean");
        }
    }

    @Override
    public void initEvent() {
        if (videoRvBean != null) {
            titleStr = videoRvBean.getTitle();
            descStr = videoRvBean.getDescription();
            coverImg = videoRvBean.getCovers();
            if (coverImg != null) {
                LoadImageUtils.loadImage(URL.BASE_URL + coverImg, iv_video_image);
            }
            tv_video_title.setText(titleStr);
            tv_video_desc.setText(descStr);
        }
        topbar_back.setOnClickListener(this);
        abl_videodetail.addOnOffsetChangedListener(new MyAppBarChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if (state == State.EXPANDED) {
                    // 展开
                    ctl_videodetail.setTitle("");
                    tv_video_title.setText(titleStr);
                    tv_video_desc.setText(descStr);
                } else if (state == State.COLLAPSED) {
                    // 折叠
                    ctl_videodetail.setTitle(titleStr);
                } else {
                    // 中间状态
                    ctl_videodetail.setTitle(titleStr);
                    tv_video_title.setText("");
                    tv_video_desc.setText("");
                }
            }
        });

    }

    @Override
    public void initData() {
        titleList=new ArrayList<String>();
        fragmentList=new ArrayList<Fragment>();
        VideoTabFragment videoTabFragment=new VideoTabFragment();
        VideoDescFragment videoDescFragment=new VideoDescFragment();
        Bundle args = new Bundle();
        args.putSerializable("bean", videoRvBean);
        videoTabFragment.setArguments(args);
        videoDescFragment.setArguments(args);
        titleList.add("视频");
        titleList.add("简介");
        fragmentList.add(videoTabFragment);
        fragmentList.add(videoDescFragment);
        adapter=new VideoFragmentAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(adapter);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
        tabLayout.getTabAt(0).setCustomView(R.layout.tablayout_tab_videodetail);
        tabLayout.getTabAt(1).setCustomView(R.layout.tablayout_tab_desc);

    }

    @Override
    protected void initSetting() {
        setTopBarEnable(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.toolbar_right_menu:
                finish();
                break;
        }
    }
}
